package com.cy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.common.entity.Result;
import com.cy.common.exception.CommonException;
import com.cy.common.utils.JwtUtils;
import com.cy.pojo.Academy;
import com.cy.pojo.Department;
import com.cy.pojo.User;
import com.cy.pojo.vo.AcademyTreeVo;
import com.cy.pojo.vo.DepartmentVo;
import com.cy.pojo.vo.SearchVo;
import com.cy.service.AcademyService;
import com.cy.service.DepartmentService;
import com.cy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description 系部信息
 * @author 残影
 * @date 2022-04-19
 */
@RestController
@RequestMapping("/department")
@Api(tags = "系部信息模块")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UserService userService;
    @Autowired
    private AcademyService academyService;

    /**
     * 根据ID查询系部信息
     */
    @GetMapping("{id}")
    @ApiOperation("根据ID查询系部信息")
    public Result findById(@PathVariable Integer id) {
        Department department = departmentService.getById(id);
        if (department != null) {
            return Result.ok("查询系部信息成功", department);
        } else {
            return Result.failed(201, "没有该系部信息");
        }
    }

    /**
    * 分页查询系部信息
    */
    @ApiOperation("分页查询系部信息")
    @GetMapping("/list")
    public Result list(SearchVo searchVo) {
        //分页构造器
        Page<Department> buildPage = new Page<>(searchVo.getPage(), searchVo.getLimit());
        //条件构造器
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(searchVo.getName())) {
            queryWrapper.like("department_name", searchVo.getName());
        }
        IPage<Department> pageList = departmentService.page(buildPage, queryWrapper);
        return Result.ok("查询成功", pageList.getRecords()).put("total", pageList.getTotal());
    }


    /**
     * 查询所有系部信息
     */
    @ApiOperation("查询所有系部信息")
    @GetMapping("/listAll")
    public Result listAll() {
        return Result.ok("查询成功", departmentService.list());
    }


    /**
     * 保存系部信息
     */
    @PostMapping
    @ApiOperation("保存系部信息")
    public Result save(@RequestBody DepartmentVo departmentVo,HttpServletRequest request) {
        Long currentUserId = JwtUtils.getUserIdByRequest(request);
        User currentUser = userService.getById(currentUserId);

        Department department = new Department();
        BeanUtils.copyProperties(departmentVo,department);
        department.setCreateTime(new Date());
        department.setUpdateTime(new Date());
        department.setCreateBy(currentUser.getNickName());
        department.setUpdateBy(currentUser.getNickName());

        boolean b = departmentService.save(department);
        if (b) {
            return Result.ok("保存用户系部信息成功", department);
        } else {
            return Result.failed("保存系部信息失败");
        }
    }

    /**
     * 修改系部信息
     */
    @PutMapping("{id}")
    @ApiOperation("修改系部信息")
    public Result update(@RequestBody DepartmentVo departmentVo,@PathVariable Long id,HttpServletRequest request) {
        Long currentUserId = JwtUtils.getUserIdByRequest(request);
        User currentUser = userService.getById(currentUserId);
        Department department = departmentService.getById(id);
        if (ObjectUtils.isEmpty(department)){
            throw new CommonException("系部信息不存在");
        }
        BeanUtils.copyProperties(departmentVo,department);
        department.setUpdateTime(new Date());
        department.setUpdateBy(currentUser.getNickName());

        boolean b = departmentService.saveOrUpdate(department);
        if (b) {
            return Result.ok("修改系部信息成功", department);
        } else {
            return Result.failed("修改系部信息失败");
        }
    }

    /**
     * 删除系部信息
     */
    @DeleteMapping("{id}")
    @ApiOperation("删除系部信息")
    public Result delete(@PathVariable Long id) {
        boolean b = departmentService.removeById(id);
        if (b) {
            return Result.ok("删除系部信息成功");
        } else {
            return Result.failed("删除系部信息失败");
        }
    }
}
